package com.grq.mis.satoken;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被springboot扫描，完成sa-token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginKey) {

        SaSession session = StpUtil.getSessionByLoginId(loginId);

        List<String> permissions = (List<String>) session.getAttribute("permissions");
        if (permissions == null) {
            permissions = new ArrayList<>();
        }

        return permissions;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginKey) {

        SaSession session = StpUtil.getSessionByLoginId(loginId);

        List<String> roles = (List<String>) session.getAttribute("roles");
        if (roles == null){
            roles = new ArrayList<>();
        }

        if (roles == null) {
            roles = new ArrayList<>();
        }

        return roles;
    }

}
